/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package makao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import shared.Card;
import shared.GameInfo;
import shared.MyCards;
import shared.PlayerInfo;

public class PlayerData
{

    private MakaoView makaoView = null;
    private GameInfo gameInfo = null;
    private MyCards myCards = null;
    private Boolean gameStarted = false;
    private Boolean myTurn = false;
    private String myLogin = null;
    private ArrayList<Card> thrownCardsArray = null;
    private DefaultTableModel tableModel;
    private ArrayList<PlayerInfo> previousPlayersInfo = null;

    public PlayerData(MakaoView makaoView)
    {
        this.makaoView = makaoView;
    }

    public void setLogin(String login)
    {
        this.myLogin = login;
        tableModel = (DefaultTableModel) makaoView.getPlayersTable().getModel();
    }

    public GameInfo getGameInfo()
    {
        return gameInfo;
    }

    public ArrayList<Card> getMyCards()
    {
        if (myCards != null)
        {
            return myCards.getCards();
        } else
        {
            return new ArrayList<Card>();
        }
    }

    public ArrayList<Card> getMyCardsToUndelete()
    {
        return myCards.getCards();
    }

    public void setGameInfo(GameInfo gameInfo)
    {
        makaoView.getTablePanel().setPaintableFlag(true);

        if (this.gameInfo != null)
        {
            setPreviousPlayersCardsAmount(this.gameInfo.getPlayersInfo());
        }
        this.gameInfo = gameInfo;

        if (gameInfo.getWhoseTurnLogin().equals(myLogin))
        {
            makaoView.getClock().startThread();
            makaoView.getCardsPanel().setEnabled(true);
            makaoView.getTablePanel().setEnabled(true);
            makaoView.getLabelDemand().setEnabled(true);
            makaoView.getLabelTakeCards().setEnabled(true);
            makaoView.getLabelTurnsWait().setEnabled(true);
            myTurn = true;
        } else
        {
            makaoView.getClock().stopTime();
            makaoView.getCardsPanel().setEnabled(false);
            makaoView.getTablePanel().setEnabled(false);
            makaoView.getLabelDemand().setEnabled(false);
            makaoView.getLabelTakeCards().setEnabled(false);
            makaoView.getLabelTurnsWait().setEnabled(false);
            myTurn = false;
        }

        if (previousPlayersInfo == null)
        {
            makaoView.getTablePanel().layCard(gameInfo.getTopCard());
        } else
        {
            if (previousPlayersInfo.size() == gameInfo.getPlayersInfo().size())
            {
                updateNewPlayersInfoOnInterface();
            }
        }

        updatePlayersInfoOnInterface();
        updatePlayersInfoOnTable();
    }

    private void updateNewPlayersInfoOnInterface()
    {
        boolean checkPlayersCardsAmountFlag = false;

        if (previousPlayersInfo != null)
        {
            for (int i = 0; i < previousPlayersInfo.size(); i++)
            {
                if (!checkPlayersCardsAmountFlag)
                {
                    for (int j = 0; j < previousPlayersInfo.size(); j++)
                    {
                        if (previousPlayersInfo.get(i).getLogin().equals(gameInfo.getPlayersInfo().get(j).getLogin()))
                        {
                            if (previousPlayersInfo.get(i).getCardsAmount() > gameInfo.getPlayersInfo().get(j).getCardsAmount())
                            {
                                makaoView.getTablePanel().layCard(gameInfo.getTopCard());
                                checkPlayersCardsAmountFlag = true;
                                break;
                            }
                        }
                    }
                } else
                {
                    break;
                }
            }
        } else
        {
            makaoView.getTablePanel().layCard(gameInfo.getTopCard());
        }
    }

    private void updatePlayersInfoOnInterface()
    {
        if (gameInfo.getDemandCardType() != 0)
        {
            switch (gameInfo.getDemandCardType())
            {
                case Card.FIVE:
                {
                    makaoView.getLabelDemand().setText("5");
                }
                break;
                case Card.SIX:
                {
                    makaoView.getLabelDemand().setText("6");
                }
                break;
                case Card.SEVEN:
                {
                    makaoView.getLabelDemand().setText("7");
                }
                break;
                case Card.EIGHT:
                {
                    makaoView.getLabelDemand().setText("8");
                }
                break;
                case Card.NINE:
                {
                    makaoView.getLabelDemand().setText("9");
                }
                break;
                case Card.TEN:
                {
                    makaoView.getLabelDemand().setText("10");
                }
                break;
                case Card.QUEEN:
                {
                    makaoView.getLabelDemand().setText("Dama");
                }
                break;
            }
        } else
        {
            if (gameInfo.getDemandCardSuit() != 0)
            {
                switch (gameInfo.getDemandCardSuit())
                {
                    case Card.HEART:
                    {
                        makaoView.getLabelDemand().setText("Kier");
                    }
                    break;
                    case Card.DIAMOND:
                    {
                        makaoView.getLabelDemand().setText("Karo");
                    }
                    break;
                    case Card.SPADE:
                    {
                        makaoView.getLabelDemand().setText("Pik");
                    }
                    break;
                    case Card.CLUB:
                    {
                        makaoView.getLabelDemand().setText("Trefl");
                    }
                    break;
                }

            } else
            {
                makaoView.getLabelDemand().setText("");
            }
        }

        if (gameInfo.getCardsToTake() != 0)
        {
            makaoView.getLabelTakeCards().setText(String.valueOf(gameInfo.getCardsToTake()));
        } else
        {
            makaoView.getLabelTakeCards().setText("");
        }

        makaoView.getLabelTurnsWait().setText(String.valueOf(gameInfo.getTurnToWait()));
    }

    private void updatePlayersInfoOnTable()
    {
        ListSelectionModel selectionModel = makaoView.getPlayersTable().getSelectionModel();


        Object[] rowData = new Object[5];
        tableModel.setRowCount(0);

        for (int i = 0; i < gameInfo.getPlayersInfo().size(); i++)
        {
            if (gameInfo.getPlayersInfo().get(i).getLogin().equals(gameInfo.getWhoseTurnLogin()))
            {
                rowData[0] = ">>";
            } else
            {
                rowData[0] = "";
            }
            rowData[1] = gameInfo.getPlayersInfo().get(i).getLogin();
            rowData[2] = gameInfo.getPlayersInfo().get(i).getPoints();
            rowData[3] = gameInfo.getPlayersInfo().get(i).getTurnsToWait();
            rowData[4] = gameInfo.getPlayersInfo().get(i).getCardsAmount();

            tableModel.addRow(rowData);


        }

        for (int i = 0; i < gameInfo.getPlayersInfo().size(); i++)
        {
            if (gameInfo.getPlayersInfo().get(i).getLogin().equals(gameInfo.getWhoseTurnLogin()))
            {
                selectionModel.setSelectionInterval(i, i);
            }
        }
    }

    public Boolean isMyTurn()
    {
        return myTurn;
    }

    public void setMyCards(MyCards myCards)
    {
        this.myCards = new MyCards(new HashSet<Card>((ArrayList<Card>) myCards.getCards().clone()));
        sortMyCards();
    }

    public Boolean isGameStarted()
    {
        return gameStarted;
    }

    public void setGameStart(Boolean value)
    {
        this.gameStarted = value;
    }

    public void revalidiateData()
    {
    }

    public void rollbackThrownCards()
    {
        if (thrownCardsArray != null)
        {
            makaoView.getCardsPanel().undeleteCards(thrownCardsArray);
        }
        thrownCardsArray = null;
        sortMyCards();
    }

    public void clearThrownCardsArray()
    {
        thrownCardsArray = null;
    }

    public void setThrownCards(ArrayList<Card> cards)
    {
        this.thrownCardsArray = cards;
    }

    public void clearPlayerData()
    {
        myTurn = false;
        myLogin = null;
        thrownCardsArray = null;
        myCards = null;
        gameInfo = null;
        gameStarted = false;
        if (tableModel != null)
        {
            tableModel.setRowCount(0);
        }

        if (previousPlayersInfo != null)
        {
            previousPlayersInfo = null;
        }
        makaoView.getLabelDemand().setEnabled(false);
        makaoView.getLabelTakeCards().setEnabled(false);
        makaoView.getLabelTurnsWait().setEnabled(false);
        makaoView.getLabelDemand().setText("");
        makaoView.getLabelTakeCards().setText("");
        makaoView.getLabelTurnsWait().setText("0");

        makaoView.getTablePanel().clearTable();
    }

    public void clearPlayerDataToNextgame()
    {
        myTurn = false;
        thrownCardsArray = null;
        myCards = null;
        gameInfo = null;
        tableModel.setRowCount(0);
        previousPlayersInfo = null;
        makaoView.getLabelDemand().setEnabled(false);
        makaoView.getLabelTakeCards().setEnabled(false);
        makaoView.getLabelTurnsWait().setEnabled(false);
        makaoView.getLabelDemand().setText("");
        makaoView.getLabelTakeCards().setText("");
        makaoView.getLabelTurnsWait().setText("0");

        makaoView.getTablePanel().clearTable();
    }

    public void removeCard(int index)
    {
        myCards.getCards().remove(index);
        sortMyCards();
    }

    public void setPreviousPlayersCardsAmount(ArrayList<PlayerInfo> playersInfo)
    {
        if (previousPlayersInfo == null)
        {
            previousPlayersInfo = new ArrayList<PlayerInfo>();
        }

        previousPlayersInfo.clear();
        for (PlayerInfo playerInfo : playersInfo)
        {
            previousPlayersInfo.add(playerInfo);
        }
    }

    public void sortMyCards()
    {
        for (int i = 0; i < myCards.getCards().size(); i++)
        {
            for (int j = 0; j < myCards.getCards().size(); j++)
            {
                if (myCards.getCards().get(j).getType() > myCards.getCards().get(i).getType())
                {
                    Collections.swap(myCards.getCards(), i, j);
                }
            }
        }

    }
}
